<!-- 分类查询活动列表页面 -->
<template>
	<view class="home-page">

		<!-- 活动推荐标题 -->
		<text class="recommend-title">{{type}}</text>

		<!-- 活动列表 -->
		<view class="activity-list">
			<view class="activity-item" v-for="(activity, index) in activityList" :key="index">
				<view @tap="goToDetail(activity.id)">
					<image class="activity-img" :src="activity.poster" mode="widthFix"></image>
					<view class="activity-info">
						<view class="activity-title">{{ activity.title }}</view>
						<view class="activity-detail">活动描述：{{ activity.description }}</view>
						<!-- 使用格式化后的时间 -->
						<view class="activity-time">
							<view class="time-item">
								<image class="time-icon" src="/static/images/calendar.png" mode="widthFix"></image>
								<text>开始：{{ formatTime(activity.startTime) }}</text>
							</view>
							<view class="time-item">
								<image class="time-icon" src="/static/images/calendar.png" mode="widthFix"></image>
								<text>结束：{{ formatTime(activity.endTime) }}</text>
							</view>
						</view>
						<view class="activity-point">
							<image class="point-icon" src="/static/images/point.png" mode="widthFix"></image>
							<text>积分数：{{ activity.point }}</text>
						</view>
					</view>
				</view>
				<view class="button-group">
					<button class="action-btn" style="backgroundColor: skyblue" @tap="handleAction(activity)"
						v-if="activity.privateStatus == 0">
						报名
					</button>
					<button class="action-btn" style="backgroundColor: #FF8888" @tap="cancelAction(activity)" v-else>
						取消报名
					</button>

				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	// 导入onShow
	// import { onShow } from 'vue'
	import {
		useRoute
	} from 'vue-router';
	// 导入activityCategoryApi
	import {
		activityCategoryApi,
		joinActivityApi,
		cancelActivityApi
	} from '../../http/home.js';
	// 导入 onShow 生命周期钩子
	import {
		onShow
	} from '@dcloudio/uni-app'
	// 导入ref
	import {
		ref
	} from 'vue'
	import dayjs from 'dayjs'

	// 获取路由参数
	const route = useRoute();

	// 分类数据
	const pageNum = 1;
	const pageSize = 10;
	const type = route.query.type + "类";
	// console.log(type);

	// 活动列表数据
	const activityList = ref([]);
	
	// 格式化时间
	const formatTime = (timeString) => {
	  return timeString ? dayjs(timeString).format('YYYY-MM-DD HH:mm') : ''
	}

	onShow(() => {
		activityCategoryApi(pageNum, pageSize, type).then(data => {
			// console.log(data);
			activityList.value = data.records;
			console.log(activityList);
		})
	})
	
	// 跳转到活动详情页
	const goToDetail = (activityId) => {
		// console.log(activityId);
		uni.navigateTo({
			url: `/pages/detail/detail?id=${activityId}`
		});
	}
	
	// 报名按钮点击事件
	const handleAction = (activity) => {
		// 更新该活动的报名状态为已报名（1）
		activity.privateStatus = 1;
		
		// console.log(activity.privateStatus);
		joinActivityApi(activity.id).then(data => {
			
			// activity.isJoin = false
			uni.showToast({
				title: '报名成功',
				icon: 'success'
			})
	
		}).catch(err => {
			console.log(err);
		})
	};
	
	// 取消报名按钮点击事件
	const cancelAction = (activity) => {
		cancelActivityApi(activity.id).then(data => {
			// 更新该活动的报名状态为未报名（0）
			activity.privateStatus = 0;
			
			// activity.isJoin = true
			uni.showToast({
				title: '取消报名成功',
				icon: 'success'
			})
	
		}).catch(err => {
			console.log(err);
		})
	};
</script>

<style scoped lang="scss">
	.home-page {
		background-color: #f8f9fa;
		min-height: 100vh;
		padding-bottom: 100rpx;
		font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
	}

	/* 搜索栏 - 添加阴影和圆角 */
	.search-bar {
		display: flex;
		align-items: center;
		background-color: #ffffff;
		padding: 20rpx 30rpx;
		margin: 20rpx;
		border-radius: 50rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
		position: relative;

		.search-input {
			flex: 1;
			height: 70rpx;
			font-size: 28rpx;
			padding-left: 60rpx;
		}

		.search-icon {
			width: 40rpx;
			height: 40rpx;
			position: absolute;
			left: 50rpx;
		}
	}

	/* 分类导航 - 添加悬停效果和间距 */
	.category-nav {
		display: flex;
		justify-content: space-around;
		padding: 30rpx 0;
		margin: 20rpx;
		background-color: #ffffff;
		border-radius: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);

		.category-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			transition: transform 0.2s ease;

			&:active {
				transform: scale(0.95);
			}

			.category-icon {
				width: 80rpx;
				height: 80rpx;
				margin-bottom: 15rpx;
			}

			.category-text {
				font-size: 26rpx;
				color: #555;
				font-weight: 500;
			}
		}
	}

	/* 活动推荐标题 - 添加装饰线 */
	.recommend-title {
		display: block;
		font-size: 36rpx;
		font-weight: 700;
		margin: 40rpx 30rpx 20rpx;
		color: #333;
		position: relative;
		padding-left: 20rpx;

		&::before {
			content: "";
			position: absolute;
			left: 0;
			top: 50%;
			transform: translateY(-50%);
			width: 8rpx;
			height: 36rpx;
			background: linear-gradient(to bottom, #4e9eff, #007aff);
			border-radius: 8rpx;
		}
	}

	/* 活动列表 - 卡片化设计 */
	.activity-list {
		padding: 0 20rpx;
		margin-bottom: 40rpx;

		.activity-item {
			background-color: #ffffff;
			border-radius: 24rpx;
			overflow: hidden;
			margin-bottom: 30rpx;
			box-shadow: 0 6rpx 24rpx rgba(0, 0, 0, 0.06);
			transition: transform 0.3s ease, box-shadow 0.3s ease;

			&:active {
				transform: translateY(-4rpx);
				box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.1);
			}

			.activity-img {
				width: 100%;
				height: 300rpx;
				display: block;
				object-fit: cover;
			}

			.activity-info {
				padding: 30rpx;

				.activity-title {
					font-size: 32rpx;
					font-weight: 700;
					margin-bottom: 15rpx;
					color: #333;
					line-height: 1.4;
				}

				.activity-detail {
					font-size: 26rpx;
					color: #666;
					margin-bottom: 10rpx;
					line-height: 1.5;
				}

				/* 新增：时间区域样式 */
				.activity-time {
					background-color: #f8f9ff;
					border-radius: 12rpx;
					padding: 16rpx 20rpx;
					margin: 20rpx 0;
					border-left: 4rpx solid #4e9eff;

					.time-item {
						display: flex;
						align-items: center;
						font-size: 26rpx;
						color: #555;
						margin-bottom: 8rpx;

						&:last-child {
							margin-bottom: 0;
						}

						.time-icon {
							width: 30rpx;
							height: 30rpx;
							margin-right: 10rpx;
						}
					}
				}

				/* 新增：积分区域样式 */
				.activity-point {
					display: flex;
					align-items: center;
					font-size: 26rpx;
					color: #ff8c00;
					background-color: #fff9f0;
					border-radius: 12rpx;
					padding: 12rpx 16rpx;
					margin-bottom: 10rpx;

					.point-icon {
						width: 32rpx;
						height: 32rpx;
						margin-right: 8rpx;
					}
				}

				/* 按钮组样式 */
				.button-group {
					display: flex;
					justify-content: space-between;
					margin-top: 30rpx;

					.action-btn {
						flex: 1;
						margin: 0 10rpx;
						padding: 18rpx 0;
						font-size: 28rpx;
						border-radius: 50rpx;
						color: white;
						border: none;
						transition: all 0.2s ease;

						&:first-child {
							background: linear-gradient(45deg, #4e9eff, #007aff);
						}

						&:last-child {
							background: linear-gradient(45deg, #ff7e5f, #ff5252);
						}

						&:active {
							transform: scale(0.96);
						}
					}
				}
			}
		}
	}
	
	/* 响应式调整 */
	@media (min-width: 768px) {
		.home-page {
			max-width: 750px;
			margin: 0 auto;
			padding: 0 20rpx;
		}

		.activity-list {
			display: grid;
			grid-template-columns: repeat(2, 1fr);
			gap: 30rpx;
		}
	}
</style>